package com.learn.egnotice1.controller;

import com.learn.egnotice1.base.response.JsonResult;
import com.learn.egnotice1.mapper.NoticeMapper;
import com.learn.egnotice1.pojo.dto.NoticeAddParam;
import com.learn.egnotice1.pojo.dto.NoticeListQuery;
import com.learn.egnotice1.pojo.entity.Notice;
import com.learn.egnotice1.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    public JsonResult noticeAddParam(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam:"+noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(1L);

        log.debug("notice:"+notice);
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult noticeList(NoticeListQuery noticeListQuery){
        List<NoticeListVO> noticeListVOS = noticeMapper.selecetNoticeList(noticeListQuery);
        return JsonResult.ok(noticeListVOS);
    }
}
